package com.yuchen.ishop.web.admin;

import com.yuchen.ishop.entity.Dictionary;
import com.yuchen.ishop.service.admin.goods.GoodsTypeService;
import com.yuchen.ishop.web.BaseAction;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 商品分类（系统）
 */
@Controller
@RequestMapping(value = "/admin/goodsType")
public class GoodsTypeAction extends BaseAction {

    private final static Logger LOG = Logger.getLogger(GoodsTypeAction.class);

    @Autowired
    private GoodsTypeService goodsTypeService;

    @RequestMapping(value = "/index")
    public String index(ModelMap modelMap)throws Exception {
        modelMap.addAttribute("goodsTypes",goodsTypeService.find());
        return "admin/goodsType/index";
    }

    @RequestMapping(value = {"/add", "/edit"})
    public String add_edit(Long id,ModelMap modelMap)throws Exception {
        if(null == id) {
            modelMap.addAttribute("max", goodsTypeService.getMaxSort());
            return "admin/goodsType/add";
        }
        else{
            modelMap.addAttribute("goodsType",goodsTypeService.getById(id));
            return "admin/goodsType/edit";
        }
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(Dictionary dictionary)throws Exception {
        goodsTypeService.insert(dictionary);
        return "redirect:/admin/goodsType/index.do";
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public String update(Dictionary dictionary)throws Exception {
        goodsTypeService.update(dictionary,true);
        return "redirect:/admin/goodsType/index.do";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public String delete(Long id,Long pageNO)throws Exception {
        goodsTypeService.delete(id);
        return "redirect:/admin/goodsType/index.do";
    }

    @RequestMapping(value = "/sortUp",method = RequestMethod.GET)
    @ResponseBody
    public Map sortUp(Long id,Long sort)throws Exception {
        goodsTypeService.sortUp(id,sort);
        return successJson();
    }

    @RequestMapping(value = "/sortDown",method = RequestMethod.GET)
    @ResponseBody
    public Map sortDown(Long id,Long sort)throws Exception {
        goodsTypeService.sortDown(id, sort);
        return successJson();
    }

}
